package com.example.demo.common.enums;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author xcy
 * @Desc 接口错误返回枚举
 * @date 2021/3/23 23:36
 * @Version v1.0
 */
public enum ApiErrorEnum {
    CODE_0(0, "error"),
    CODE_1(1, "success"),
    API_USER_1(10001, "用户不存在或帐号密码错误"),
    API_USER_2(10002, "用户已被禁用"),
    API_USER_3(10003, "token无效"),
    API_USER_4(10004, "无效请求"),
    ;

    private int code;
    private String description;

    private final static Map<String, String> CODE_DESCRIPTION_MAP = new ConcurrentHashMap<>(ApiErrorEnum.values().length);

    ApiErrorEnum(int code, String description) {
        this.code = code;
        this.description = description;
    }

    public static String getDescriptionByCode(String code) {
        if (CODE_DESCRIPTION_MAP.size() == 0) {
            ApiErrorEnum[] values = ApiErrorEnum.values();
            for (ApiErrorEnum value : values) {
                CODE_DESCRIPTION_MAP.put(Integer.toString(value.code), value.description);
            }
        }
        return CODE_DESCRIPTION_MAP.get(code);
    }

    public int getCode() {
        return code;
    }

    public String getDescription() {
        return description;
    }
}
